<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>留言板</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .box {
            width: 800px;
            margin: auto;
        }

        .box .content {
            width: 100%;
            height: 350px;
            border: 1px solid #ccc;
            margin: 20px auto;
            padding: 10px;
            box-sizing: border-box;
        }

        .box .content li {
            margin: 10px 0;
            padding: 5px;
            line-height: 2;
            border-radius: 5px;
            clear: both;
            width: 100%;
            list-style: none;
        }

        .box textarea {
            height: 80px;
            display: block;
            margin: auto;
            margin-top: 20px;
            width: 100%;
        }

        .box button {
            width: 80px;
            height: 30px;
            border: 1px solid #ccc;
            float: right;
            margin-top: 10px;
        }

        .active {
            float: right;
            background-color: gray;
            color: #fff;
        }

        p {
            color: #ccc;
            font-size: 12px;
        }
    </style>
</head>

<body>
    <div class="box">
        <ul class="content">
            <!-- <li>
                <span style="float: left;">s</span>
                <span style="float: right;">d</span>
            </li> -->
        </ul>
        <textarea name="" id="text" cols="30" rows="10"></textarea>
        <button id="btn">提交</button>
        <p>0/20</p>
    </div>
    <script src="../js/public.js"></script>
    <script>
        let content = document.querySelector('.content');
        let text = document.querySelector('#text');
        let btn = document.querySelector('#btn');
        let p = document.querySelector('p');

        btn.onclick = function () {
            text.value ? renden() : alert('请输入内容');
            text.removeAttribute("disabled");
            p.innerHTML = "0" + "/" + "20";
        }

        window.onkeydown = function (e) {
            if (e.ctrlKey && e.keyCode == 13) text.value ? renden() : alert('请输入内容');
            text.removeAttribute("disabled");
            p.innerHTML = "0" + "/" + "20";
        }

        text.oninput = function () {
            let val = text.value;
            p.innerHTML = val.length + "/" + "20";
            if (val.length >= 20) text.setAttribute("disabled", "disabled");
        }

        function renden() {
            let date = new Date();
            let li = document.createElement('li');
            li.className = 'active';
            li.innerHTML = `<span style="float: left;">${text.value}</span>
                            <span style="float: right;">${dates(date).year}-${dates(date).month}-${dates(date).dates}&nbsp;${dates(date).hour}:${dates(date).minute}:${dates(date).second}</span>`;
            content.appendChild(li);
            text.value = ''
        }
    </script>
</body>

</html>